const CryptoJS = require("crypto-js")

function md5(input) {
    return CryptoJS.MD5(input).toString()
}

const dckE15Yk15AF = "oHLKvpN54hwpLWjt";//DESkey，可自定义
const dcik4kPiOWjo = "pdgLk9FGBd5kXbm0";//密钥偏移量IV，可自定义

var m4Sq1jVkq = 'GETTIMELIST'
ogFjn1FgMP = {
    "city": "杭州",
    "type": "HOUR",
    "startTime": "2023-12-25 10:00:00",
    "endTime": "2023-12-26 13:00:00"
}
var DES = {
    encrypt: function (text, key, iv) {
        var secretkey = md5(key).substr(0, 16);
        var secretiv = md5(iv).substr(24, 8);
        secretkey = CryptoJS.enc.Utf8.parse(secretkey);
        secretiv = CryptoJS.enc.Utf8.parse(secretiv);
        var result = CryptoJS.DES.encrypt(text, secretkey, {
            iv: secretiv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        return result.toString();
    }
}

function Base64() {
    // 编码表
    this._keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    // 编码方法
    this.encode = function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = this._utf8_encode(input);

        while (i < input.length) {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
                this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
                this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
        }

        return output;
    };

    // UTF-8 编码方法
    this._utf8_encode = function (string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            } else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            } else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
        }

        return utftext;
    };
}

function ObjectSort(obj) {
    var newObject = {};
    Object.keys(obj).sort().map(function (key) {
        newObject[key] = obj[key];
    });
    return newObject;
}

function pov0M2gfR(method, obj) {
    var appId = '271c2aab7dd615dacbadcb41d3c77fa4';
    var clienttype = 'WEB';
    var timestamp = new Date().getTime();
    // console.log(method, obj,ObjectSort(obj),appId + method + timestamp + 'WEIXIN' + JSON.stringify(ObjectSort(obj)));
    var param = {
        appId: appId,
        method: method,
        timestamp: timestamp,
        clienttype: clienttype,
        object: obj,
        secret: md5(appId + method + timestamp + clienttype + JSON.stringify(ObjectSort(obj)))
    };
    var base64 = new Base64();
    param = base64.encode(JSON.stringify(param));

    param = DES.encrypt(param, dckE15Yk15AF, dcik4kPiOWjo);
    return param;
};

function result() {
    return phqbwou = pov0M2gfR(m4Sq1jVkq, ogFjn1FgMP)
}